package com.example.child_protection.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.child_protection.dao.ChildArchivesDao;
import com.example.child_protection.dao.ChildArchivesValuesDao;
import com.example.child_protection.dao.ChildOtherInfoDao;
import com.example.child_protection.dao.SystemFeedingProgramDao;
import com.example.child_protection.dto.info.ChildArchivesDTO;
import com.example.child_protection.dto.info.ChildArchivesValuesDTO;
import com.example.child_protection.dto.info.ChildOtherInfoDTO;
import com.example.child_protection.dto.info.SystemFeedingProgramDTO;
import com.example.child_protection.dto.request.SimpleIdRequestDTO;
import com.example.child_protection.dto.request.find.ChildArchivesRequestDTO;
import com.example.child_protection.dto.response.ChildArchivesListResponseDTO;
import com.example.child_protection.dto.response.ChildArchivesResponseDTO;
import com.example.child_protection.enums.ServiceActionEnums;
import com.example.child_protection.model.ChildArchivesModel;
import com.example.child_protection.model.ChildArchivesValuesModel;
import com.example.child_protection.model.ChildOtherInfoModel;
import com.example.child_protection.model.SystemFeedingProgramModel;
import com.example.child_protection.service.ChildArchivesQueryService;
import com.example.child_protection.service.base.MyBaseService;
import com.example.child_protection.service.base.MyServiceExecutor;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 科室查询service实现层
 *
 * @author Biz
 * @date 2018-11-2
 */
@Component
public class ChildArchivesQueryServiceImpl extends MyBaseService implements ChildArchivesQueryService {

    @Autowired
    public ChildArchivesDao childArchivesDao;

    @Autowired
    public ChildArchivesValuesDao childArchivesValuesDao;

    @Autowired
    public ChildOtherInfoDao childOtherInfoDao;

    @Autowired
    public SystemFeedingProgramDao systemFeedingProgramDao;

    @Autowired
    public SystemFeedingProgramQueryServiceImpl systemFeedingProgramQueryServiceImpl;


    /**
     * id查询
     *
     * @param requestDTO
     * @return
     */
    public ChildArchivesResponseDTO findId(SimpleIdRequestDTO requestDTO) {
        return this.service(ServiceActionEnums.DEPARTMENT_FIND_ID, requestDTO, ChildArchivesResponseDTO.class, new MyServiceExecutor<SimpleIdRequestDTO, ChildArchivesResponseDTO>() {
            @Override
            protected ChildArchivesResponseDTO myprocess(SimpleIdRequestDTO requestDTO) throws Exception {
                ChildArchivesResponseDTO responseDTO = new ChildArchivesResponseDTO();
                //1.1查询档案基本信息
                ChildArchivesModel childArchivesModel = childArchivesDao.findId(requestDTO.getId());
                ChildArchivesDTO childArchives = new ChildArchivesDTO();
                BeanUtils.copyProperties(childArchivesModel, childArchives);

                //1.2查询扩容信息
                ChildOtherInfoModel childOtherInfoModel = childOtherInfoDao.findByArchivesId(requestDTO.getId());
                ChildOtherInfoDTO childOtherInfo = new ChildOtherInfoDTO();
                if(null!=childOtherInfoModel) {
                    BeanUtils.copyProperties(childOtherInfoModel, childOtherInfo);
                }
                childArchives.setOtherInfo(childOtherInfo);

                //1.3查询儿童详细信息
                ChildArchivesValuesModel valuesModel = new ChildArchivesValuesModel();
                valuesModel.setCategoryId(1000010000L);
                List<ChildArchivesValuesModel> values = childArchivesValuesDao.findPage(valuesModel);
                List<ChildArchivesValuesDTO> infoValues = JSONObject.parseArray(JSONArray.toJSONString(values), ChildArchivesValuesDTO.class);
                childArchives.setValuesInfo(infoValues);
                //1.4查询喂养方案

                SystemFeedingProgramModel req = new SystemFeedingProgramModel();
                Long timer = childArchivesModel.getAge().getTime();
                Long timer1 = new Date().getTime();

                Long s = timer1 - timer;
                Long daysCount = s/1000/60/60/24;
                Long month = daysCount/30;
                Long days = daysCount%30;
                req.setMonth(Integer.parseInt(month+""));
                req.setDays(Integer.parseInt(days+""));

                List<SystemFeedingProgramModel> lists = systemFeedingProgramDao.findPage(req);
                List<SystemFeedingProgramDTO> systemFeedingPrograms = JSONObject.parseArray(JSONArray.toJSONString(lists), SystemFeedingProgramDTO.class);
                for (SystemFeedingProgramDTO systemFeedingProgram : systemFeedingPrograms) {
                    systemFeedingProgramQueryServiceImpl.convertValues(systemFeedingProgram);
                }

                responseDTO.setInfo(childArchives);
                responseDTO.successfully();
                return responseDTO;
           }
        });
    }

    /**
     * 列表查询
     *1
     * @param requestDTO
     * @return
     */
    public ChildArchivesListResponseDTO findList(ChildArchivesRequestDTO requestDTO) {
                ChildArchivesListResponseDTO responseDTO = new ChildArchivesListResponseDTO();
                ChildArchivesModel req = new ChildArchivesModel();
                BeanUtils.copyProperties(requestDTO, req);
                List<ChildArchivesModel> lists = childArchivesDao.findPage(req);
                List<ChildArchivesDTO> roles = JSONObject.parseArray(JSONArray.toJSONString(lists), ChildArchivesDTO.class);
                responseDTO.setInfos(roles);
                responseDTO.successfully();
                return responseDTO;
    }
}
